<template>
  <ContentWrap>
    <el-descriptions :column="1" border>
      <el-descriptions-item label="申请人姓名">
        {{ detailData.userName }}
      </el-descriptions-item>
      <el-descriptions-item label="采购单号">
        {{ detailData.orderNo }}
      </el-descriptions-item>
      <el-descriptions-item label="采购标题">
        {{ detailData.title }}
      </el-descriptions-item>
      <el-descriptions-item label="备注">
        {{ detailData.remarks }}
      </el-descriptions-item>
      <el-descriptions-item label="审批结果">
        <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="detailData.status ?? ''" />
      </el-descriptions-item>
    </el-descriptions>

    <!-- 采购明细Tab -->
    <el-tabs v-model="activeTab" class="mt-20px" type="border-card">
      <el-tab-pane label="采购明细" name="items">
        <el-table :data="itemList" v-loading="itemLoading" style="width: 100%">
          <el-table-column label="物料编码" prop="materialCode" />
          <el-table-column label="物料名称" prop="materialName" />
          <el-table-column label="物料类型" prop="materialType">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.OA_MATERIAL_TYPE" :value="scope.row.materialType ?? '' " />
            </template>
          </el-table-column>

          <el-table-column label="单位" prop="unitType">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.OA_ASSET_UNIT" :value="scope.row.unitType" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="quantity" />
          <el-table-column label="单价" prop="price" />
          <el-table-column label="总价" prop="totalPrice" />
          <el-table-column label="备注" prop="remarks" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>
<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { propTypes } from '@/utils/propTypes'
import { PurchaseOrderApi } from '@/api/oa/purchaseorder'
import { PurchaseOrderItemApi } from '@/api/oa/purchaseorder'

const { query } = useRoute() // 查询参数

const props = defineProps({
  id: propTypes.number.def(undefined)
})
const detailLoading = ref(false) // 表单的加载中
const detailData = ref<any>({}) // 详情数据
const queryId = query.id as unknown as number // 从 URL 传递过来的 id 编号

const activeTab = ref('items')
const itemList = ref([])
const itemLoading = ref(false)

/** 获得数据 */
const getInfo = async () => {
  detailLoading.value = true
  try {
    detailData.value = await PurchaseOrderApi.getPurchaseOrder(props.id || queryId)
    await getItemList()
  } finally {
    detailLoading.value = false
  }
}

/** 获取采购明细列表 */
const getItemList = async () => {
  itemLoading.value = true
  try {
    const res = await PurchaseOrderItemApi.getPurchaseOrderItemPage({
      purchaseOrderId: props.id || queryId,
      pageNo: 1,
      pageSize: 100
    })
    itemList.value = res.list || []
  } finally {
    itemLoading.value = false
  }
}

defineExpose({ open: getInfo }) // 提供 open 方法，用于打开弹窗

/** 初始化 **/
onMounted(() => {
  getInfo()
})
</script>
